package com.yc.jpyy.mobile.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/student")
public class WGetReleaseCourseController extends BaseController {

	@Autowired
	private BizService bizService;

	@Autowired
	private BizDao bizDao;
	private static Logger logger = LoggerFactory.getLogger(BaseController.class);
	
	/*
	 * 已完成/预约/取消课程
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getCourseDetailByStatus", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getCourseDetailByStatus(HttpServletRequest request) {

		RestResponse response = new RestResponse();
		
		try {
			// 用户Id
			String studentId = request.getParameter("studentId");
			//课程状态0:已完成 1：已预约  2：已取消
			String status = request.getParameter("status");
			
			if ((null != studentId && !studentId.isEmpty()) &&
					null != status && !status.isEmpty()) {
				Map<String,Object> paramMap = new HashMap<String,Object>();
				paramMap.put("studentId", studentId);
				paramMap.put("status", status);
				paramMap.put("imageUrl", Config.getImageAddress());
				//查询课程信息
				List<Map<String, Object>> courseDetails = bizService.getCourseDetailByStatus(paramMap);
				if (null != courseDetails && courseDetails.size() > 0) {
					response.setData(courseDetails);
				} else {
					response.setErrorcode(1);
					response.setMessage("无课程信息！");
				}
			} else {
				response.setErrorcode(1);
				response.setMessage("必填字段为空");
			}
		} catch (Exception e) {
			logger.info("getCourseDetailByStatus:"+ e.toString());
			response.setReturnMsg(1,"查询失败！");
		} finally {
			return response.toJSON();
		}

	}
	
	

	/*
	 * 未开课查询接口 参数： 当前日期·
	 *
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/NoCourseHistory", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String NoCourseHistory(HttpServletRequest request) {

		RestResponse response = new RestResponse();

		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = format.format(date);
		Map<String, Object> paramsMap = new HashMap<String, Object>();

		try {
			String studentId = request.getParameter("studentId");

			if (!today.isEmpty() && today != null && studentId != null
					&& !studentId.isEmpty() ) {

				paramsMap.put("studentId", studentId);
				paramsMap.put("today", today);

				// 拿到未开课程所有信息
				List<Map<String, Object>> teaInfo = bizDao
						.NoCourseHistory(paramsMap);

				if (null!=teaInfo&&teaInfo.size() > 0) {
					response.setData(teaInfo);
				} else {
					response.setErrorcode(1);
					response.setMessage("未开课无数据！");
				}
			} else {
				response.setErrorcode(1);
				response.setMessage("请先绑定！");
			}

		} catch (Exception e) {
			logger.info("NoCourseHistory:"+ e.toString());
			response.setReturnMsg(1,"查询出现异常！");
		} finally {
			return response.toJSON();
		}

	}

	/*
	 * 待评价课程
	 */

	@SuppressWarnings("finally")
	@RequestMapping(value = "/waitCourseHistory", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String waitCourseHistory(HttpServletRequest request) {

		RestResponse response = new RestResponse();

		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = format.format(date);
		Map<String, Object> paramsMap = new HashMap<String, Object>();

		try {
			// 用户Id
			String studentId = request.getParameter("studentId");
			if (!today.isEmpty() && today != null && studentId != null&&!studentId.isEmpty()) {

				paramsMap.put("studentId", studentId);
				paramsMap.put("today", today);

				// 拿到待评价程所有信息
				List<Map<String, Object>> teaInfo = bizDao
						.waitCourseHistory(paramsMap);

				if (null!=teaInfo&&teaInfo.size() > 0) {

					response.setData(teaInfo);

				} else {
					response.setErrorcode(1);
					response.setMessage("待评价无数据！");
				}

			}else {
				response.setErrorcode(1);
				response.setMessage("请先绑定！");
			}

		} catch (Exception e) {

			logger.info("waitCourseHistory:"+ e.toString());
			response.setReturnMsg(1,"查询出现异常！");

		} finally {

			return response.toJSON();
		}

	}

	/*
	 * 已完成课程
	 */

	@SuppressWarnings("finally")
	@RequestMapping(value = "/finishCourseHistory", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String finishCourseHistory(HttpServletRequest request) {

		RestResponse response = new RestResponse();

		try {
			// 用户Id
			String studentId = request.getParameter("studentId");
			if (null != studentId && !studentId.isEmpty()) {	 
				// 拿到已完成课程所有信息
				List<Map<String, Object>> teaInfo = bizDao.finishCourseHistory(studentId);
				if (null!=teaInfo&&teaInfo.size() > 0) {
					response.setData(teaInfo);
				} else {
					response.setErrorcode(1);
					response.setMessage("已完成无数据！");
				}
			} else {
				response.setErrorcode(1);
				response.setMessage("请先绑定！");
			}

		} catch (Exception e) {

			response.setErrorcode(1);
			response.setMessage("查询出现异常！");
			System.out.println(e);

		} finally {

			return response.toJSON();
		}

	}
}
